package com.dlc.shop.poly.api.controller;


import com.linchuang.seneca.enums.PolyLogisticsEnum;
import com.linchuang.seneca.exception.PolyMallException;
import com.linchuang.seneca.request.PolyMallCommonGetOrderTraceRequest;
import com.linchuang.seneca.request.PolyMallGetOrderTraceRequest;
import com.linchuang.seneca.response.PolyMallGetOrderTraceResponse;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.poly.PolyMp;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.config.ShopConfig;
import com.dlc.shop.poly.common.service.PolyMallGetOrderTraceService;
import com.dlc.shop.poly.common.service.PolyMallService;
import com.dlc.shop.security.api.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("GetOrderTrace")
@RequestMapping("/getOrderTrace")
@Tag(name = "查询快递节点")
@AllArgsConstructor
public class PolyMallGetOrderTraceController {

    private final ShopConfig shopConfig;
    private final PolyMallService service;
    private final PolyMallGetOrderTraceService polyMallGetOrderTraceService;
    /**
     * 查询物流信息
     *
     * @param request 查询物流信息
     * @return 查询物流信息
     */
    @GetMapping("/getOrderTrace")
    @Operation(summary = "查询物流信息" , description = "查询物流信息")
    public ServerResponseEntity getGetOrderTrace(PolyMallGetOrderTraceRequest request) throws PolyMallException, PolyMallException {
        String userId = SecurityUtils.getUser().getUserId();
        PolyMp polyMp = shopConfig.getPoly();
        String logisticType = request.getLogisticType();
        String code = PolyLogisticsEnum.getCodeByName(logisticType);

        if (code == null){
             logisticType = logisticType.replace("快递", "");
            code = PolyLogisticsEnum.getCodeByName(logisticType);
        }
        if (code == null ){
            throw new YamiShopBindException("yami.delivery.logistics.check");
        }
        //拿到数据后开始查询
        PolyMallCommonGetOrderTraceRequest polyMallCommonGetOrderTraceRequest = new PolyMallCommonGetOrderTraceRequest();
        polyMallCommonGetOrderTraceRequest.setAppkey(polyMp.getAppKey());
        polyMallCommonGetOrderTraceRequest.setAppSecret(polyMp.getAppSecret());
        polyMallCommonGetOrderTraceRequest.setToken(polyMp.getToken());
        polyMallCommonGetOrderTraceRequest.setBizcontent(request.toString());
        String sign = service.generateSign(polyMallCommonGetOrderTraceRequest);
        polyMallCommonGetOrderTraceRequest.setSign(sign);

        PolyMallGetOrderTraceResponse response = polyMallGetOrderTraceService.getGetOrderTrace(polyMallCommonGetOrderTraceRequest);

        return ServerResponseEntity.success(response);
    }



}
